<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
//use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;

class Controller extends BaseController
{
    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;

    protected static function successForJson($data = '')
    {
        return response()->json(['status' => 1, 'msg' => '', 'data' => $data]);
    }

    protected static function failForJson($msg)
    {
        return response()->json(['status' => 0, 'msg' => $msg, 'data' => '']);
    }

    protected static function doRequest($url, $param = array())
    {

        $urlinfo = parse_url($url);

        $host = $urlinfo['host'];
        $path = $urlinfo['path'];
        $query = isset($param) ? http_build_query($param) : '';

        $port = 8000;
        $errno = 0;
        $errstr = '';
        $timeout = 10;

        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

        $out = "GET " . $path . " HTTP/1.1\r\n";
        $out .= "host:" . $host . "\r\n";
//        $out .= "content-length:" . strlen($query) . "\r\n";
//        $out .= "content-type:application/x-www-form-urlencoded\r\n";
        $out .= "connection:close\r\n\r\n";
        $out .= $query;

        fwrite($fp, $out);

        fclose($fp);
    }
}
